import {Button, message, Modal, Popconfirm, Table} from "antd";
import {useLesson} from "@/hooks/useLesson";
import {ActionType, ProColumns} from "@ant-design/pro-components";
import {listLicenseTypeAPI} from "@/pages/license-type/services";
import {
  deleteStudentAPI,
  pageByLessonAPI,
  pageStudentAPI,
  studentArriveLessonApi
} from "@/pages/student-manager/services";
import ProTable from "@ant-design/pro-table";
import {useRef} from "react";

interface ILessonStudentModal {
  visible: boolean;
  onCancel: () => void;
  lesson: API.ILesson
}

const LessonStudentModal: React.FC<ILessonStudentModal> = ({lesson, visible, onCancel}) => {

  const {lessonTitle} = useLesson(lesson)
  const actionRef = useRef<ActionType>();

  const columns: ProColumns<API.IStudent>[] = [
    {
      title: 'ID',
      dataIndex: 'id',
      hideInSearch: true,
    },
    {
      title: '姓名',
      dataIndex: 'name',
    },
    {
      title: '报名类型',
      dataIndex: 'enrollmentType',
      hideInSearch: true,
      request: async () => {
        const {data} = await listLicenseTypeAPI({});
        console.log('data', data)
        return (data || []).map((item: API.ILicenseType) => {
          return {
            label: item.licenseCode,
            value: item.licenseCode,
          }
        })
      },
    },
    {
      title: '学籍状态',
      dataIndex: 'learningStatus',
      hideInSearch: true,
      valueEnum: {
        1: '在籍',
        2: '休学',
        3: '退学',
        4: '毕业',
      }
    },
    {
      title: '出席状态',
      dataIndex: 'signInStatus',
      hideInSearch: true,
      valueType: 'select',
      valueEnum: {
        "1": {text: '未签到', status: 'Processing'},
        "2": {text: '已签到', status: 'Success'},
        "3": {text: '已取消', status: 'Error'},
      }
    },
    {
      title: '操作',
      valueType: 'option',
      render: (_, record) => [
        <Popconfirm
          key="detail"
          title="确认学员已到场？"
          onConfirm={async () => {
            const {data} = await studentArriveLessonApi({studentId: record.id, lessonId: lesson.id})
            if (data) {
              message.success('签到成功')
              actionRef.current?.reload()
            }
          }}
        >
          <Button
            type="text"
            disabled={record.signInStatus === '2' || record.signInStatus === '3'}
          >
            签到
          </Button>
        </Popconfirm>
      ],
    },
  ];

  return (
    <Modal
      title={`${lessonTitle} - 学员列表`}
      visible={visible}
      onCancel={onCancel}
      footer={null}
      width={800}
    >
      <ProTable<API.IStudent>
        headerTitle="学员列表"
        rowKey="id"
        search={{
          labelWidth: 120,
          filterType: 'light',
        }}
        actionRef={actionRef}
        size={'small'}
        request={async (params) => {
          // 实际项目中替换为API调用
          const {data} = await pageByLessonAPI({...params, lessonId: lesson.id});
          return data
        }}
        columns={columns}
        pagination={{
          pageSize: 10,
        } as any}
      />
    </Modal>
  )
}

export default LessonStudentModal
